layui.use(['jquery', 'element', 'form'], function () {
    var $ = layui.$; // 使用jquery
    var element = layui.element; // 获得element模块
    var form = layui.form; // 获得form模块

    // 判断输入当前密码是否正确
    $('input[name="currentlpass"]').blur(function () {
        var currentlpass = $(this);

        // AJAX传输
        $.ajax({
            type: "post",
            url: "/admin/selestuserlpass.do",
            async: true,
            data: {
                "lpass": currentlpass.val()
            },
            dataType: "json",
            error: function () {
                layer.msg("ajax请求失败", {
                    icon: 5
                });
            },
            success: function (response) {
                if (response != 1) {
                    layer.msg("密码不存在", {
                        icon: 5
                    });
                    currentlpass.val("");
                }
            }
        });
    });

    // 提交表单
    form.on('submit(formDemo)', function (data) {
        if (data.field.checklpass != data.field.newlpass) {
            layer.msg('新密码不一致', {
                icon: 5
            });
            return false;
        }

        // AJAX传输
        $.ajax({
            type: "post",
            async: false,
            url: "/admin/upuserpass.do",
            data: {
                "lpass": data.field.newlpass,
            },
            dataType: "text",
            error: function () {
                layer.msg("ajax请求失败", {
                    icon: 5
                });
            },
            success: function (response) {
                if (response == 1) {
                    layer.msg("密码修改成功", {
                        icon: 6
                    });
                } 
            }
        });

        // 阻止form默认行为
        return false;
    });

});